import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/tabbar/underline/gt_tabs.dart';

/// @Title   Thirdly类型的tab
///   文字大小11，tab间隔2，tab高度20
/// @Author: bryce
/// @CreateDate: 2023年10月11日20:21:34
enum GTThirdlyTabBarSizeV3 { text14, text12 }

class GTThirdlyTabBarV3 extends StatelessWidget {
  final List<String>? tabTitles;
  final List<GTTabModalV3>? tabData;
  final TabController? controller;
  final EdgeInsetsGeometry? padding;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;

  final bool? isCapsule;
  final bool? showDivider;
  final CrossAxisAlignment? crossAxisAlignment;
  final GTThirdlyTabBarSizeV3? tabSize;

  const GTThirdlyTabBarV3({
    super.key,
    this.tabTitles,
    this.tabData,
    this.controller,
    this.padding,
    this.onInterceptSwitch,
    this.onInterceptTapSwitch,
    this.isCapsule = false,
    this.showDivider = false,
    this.crossAxisAlignment,
    this.tabSize = GTThirdlyTabBarSizeV3.text14,
  });

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle;
    double textSize;
    double indicatorPaddingLeft, indicatorPaddingRight;
    bool _showDivider = showDivider ?? false;

    if (isCapsule ?? false) {
      textStyle = gtTextStyleW500();
      textSize = 12;
      indicatorPaddingLeft = 0;
      indicatorPaddingRight = 0;
      _showDivider = false;
    } else {
      textStyle = gtTextStyleW400();
      textSize = (tabSize ?? GTThirdlyTabBarSizeV3.text14) == GTThirdlyTabBarSizeV3.text14 ? 14 : 12;
      indicatorPaddingLeft = 0;
      indicatorPaddingRight = 16;
    }
    return Column(
      crossAxisAlignment: crossAxisAlignment ?? CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        GTTabBar(
            dividerColor: Colors.transparent,
            controller: controller,
            isScrollable: true,
            indicatorWeight: 0.1,
            padding: padding,
            unselectedLabelColor: GTColorsV3.uikitText3V3.color,
            labelColor: GTColorsV3.uikitText1V3.color,
            labelStyle: textStyle.copyWith(fontSize: textSize).copyWith(height: 1),
            unselectedLabelStyle: textStyle.copyWith(fontSize: textSize).copyWith(height: 1),
            labelPadding: GTEdgeInsetsOnly(left: indicatorPaddingLeft, right: indicatorPaddingRight),
            overlayColor: MaterialStateProperty.resolveWith<Color?>(
              (Set<MaterialState> states) => Colors.transparent,
            ),
            onInterceptSwitch: onInterceptSwitch,
            onInterceptTapSwitch: onInterceptTapSwitch,
            indicator: (isCapsule ?? false)
                ? BoxDecoration(
                    color: Colors.transparent,
                    border: Border.all(
                      color: GTColorsV3.text1.color,
                      width: 1,
                    ),
                    borderRadius: BorderRadius.circular(43),
                  )
                : const BoxDecoration(color: Colors.transparent),
            tabs: tabs()),
        if (_showDivider) ...[
          Container(
            height: 1,
            color: GTColorsV3.divider2.color,
          ),
        ]
      ],
    );
  }

  List<Widget> tabs() {
    if (tabTitles != null && tabTitles?.isNotEmpty == true) {
      return tabTitles!.map((e) => _tabItem(e)).toList();
    } else if (tabData != null && tabData?.isNotEmpty == true) {
      return tabData!.map((e) => _tabDataItem(e)).toList();
    } else {
      return [];
    }
  }

  Widget _tabItem(String title) {
    return GTTab(
      height: (isCapsule ?? false) ? 24 : 30,
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: (isCapsule ?? false) ? 12 : 0),
        child: Text(title,
            textHeightBehavior:
                const TextHeightBehavior(applyHeightToLastDescent: true, applyHeightToFirstAscent: true)),
      ),
    );
  }

  Widget _tabDataItem(GTTabModalV3 bean) {
    return GTTab(
      height: (isCapsule ?? false) ? 24 : 30,
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: (isCapsule ?? false) ? 12 : 0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (bean.leftIcon != null) ...[
              bean.leftIcon!,
              SizedBox(width: bean.iconPadding),
            ],
            Text(bean.text,
                textHeightBehavior:
                    const TextHeightBehavior(applyHeightToLastDescent: true, applyHeightToFirstAscent: true)),
            if (bean.rightIcon != null) ...[
              SizedBox(width: bean.iconPadding),
              bean.rightIcon!,
            ],
          ],
        ),
      ),
    );
  }
}

class GTTabModalV3 {
  final String text;
  final Widget? leftIcon;
  final Widget? rightIcon;
  final double? iconPadding;
  final GlobalKey? widgetKey;

  GTTabModalV3({required this.text, this.leftIcon, this.rightIcon, this.iconPadding, this.widgetKey});
}
